package com.gitee.wsl.func.procedure.sample

import com.gitee.wsl.func.procedure.Procedure

/**
 * Creates a PairImpl of objects and their indexes and adds the result to a target collection.
 */
class ZipWithIndexProcedure<T, R : MutableCollection<Pair<T, Int>>>(
    private val target: R
) : Procedure<T> {
    private var index = 0

    override fun value(each: T) {
        this.target.add(each to this.index)
        this.index += 1
    }

    companion object {

        fun <TT, RR : MutableCollection<Pair<TT, Int>>> create(target: RR): ZipWithIndexProcedure<TT, RR> {
            return ZipWithIndexProcedure(target)
        }
    }
}
